import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SortAndFormatText {
public static int DEFAULT_TYPESETTIBG = 20;
    public static void main(String[] args) {
        String inputFilePath = "input_不规则复数、现在分词、过去式、过去分词.txt"; // 输入文件路径
        String outputFilePath = "output_不规则复数、现在分词、过去式、过去分词.txt"; // 输出文件路径

        try {
            List<String> lines = readLinesFromFile(inputFilePath);
            List<String> sortedLines = sortLinesByFirstLetter(lines);
            writeLinesToFile(sortedLines, outputFilePath);
            System.out.println("文件排序和格式化完成！\n"+ outputFilePath);
        } catch (IOException e) {
            System.err.println("发生错误：" + e.getMessage());
        }
    }

    // 从文件中读取所有行
    private static List<String> readLinesFromFile(String filePath) throws IOException {
        List<String> lines = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }
        return lines;
    }

    // 按每行的首字母排序
    private static List<String> sortLinesByFirstLetter(List<String> lines) {
        lines.sort(Comparator.comparing(line -> {
            if (line == null || line.isEmpty()) {
                return ' '; // 空行排在最后
            }
            char firstChar = line.charAt(0);
            return Character.toLowerCase(firstChar); // 忽略大小写
        }));
        return lines;
    }

    // 将排序后的行写入文件，并进行格式化
    private static void writeLinesToFile(List<String> lines, String filePath) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String line : lines) {
                if (line.trim().isEmpty()) {
                    // writer.newLine(); // 处理空行
                    continue;
                }

                // String str = "arise  第三人称单数:arises  现在分词:arising";
                int lastEnd = 0;
                Pattern pattern = Pattern.compile("^\\b\\w+(?=\\s*)"); // 使用逗号作为分隔符
                // String[] parts = pattern.split(str);
                     ArrayList<String> parts = new ArrayList<String>();
                    Matcher mat = pattern.matcher(line);
                    while (mat.find()) {
                        System.out.println(mat.end());
                        parts.add(line.substring(lastEnd, mat.end()).trim());
                        lastEnd = mat.end();
                        parts.add(line.substring(mat.end(),line.length() ).trim());
                    }
                    for (String part : parts) {
                         System.out.println(part); // 输出：Hello
                    }

        try {
                    System.out.println("\r\r\r\n");
                String formattedLine = formatLine(parts.get(0),parts.get(1));
                writer.write(formattedLine);
                writer.newLine();
        } catch (Exception e) {
                String formattedLine = formatLine(line,"");
                writer.write(formattedLine);
                writer.newLine();
         
        }
            }
        }
    }

    // 格式化行：如果首字母是字母，则补齐到12个字符
    private static String formatLine(String line1,String line2) {
        if (line1 == null || line1.isEmpty()) {
            return "";
        }
        char firstChar = line1.charAt(0);
        if (Character.isLetter(firstChar)) {
            return String.format("%-"+DEFAULT_TYPESETTIBG+"s", line1) + line2; // 左对齐，补空格到12个字符
        } else {
            return line1; // 非字母行不处理
        }
    }

}
